package gui.configuration;

import gui.WuselPanelInterface;

import importExport.log.WuselLogger;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Properties;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;

import language.LanguageProfiler;
import javax.swing.JTextField;

import dataTypes.Constants;

/**
 * @author Kevin Wagner
 * @version 1.0
 * 
 */
public class ConfigPanel extends JPanel implements ActionListener,
		WuselPanelInterface {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4327526735864723755L;
	private LanguageProfiler lp;
	private Properties props;
	private JLabel langL;
	private JComboBox langCB = new JComboBox();
	private JButton saveBtn, disDirBtn, envDirBtn, scenDirBtn;
	private JTextField disDirT, envDirT, scenDirT;
	private JLabel disDirL, envDirL, scenDirL;

	/**
	 * Create the panel.
	 * 
	 * @param props
	 * 
	 * @param lp
	 */
	public ConfigPanel(Properties props, LanguageProfiler lp) {
		this.lp = lp;
		this.props = props;
		langL = new JLabel();

		saveBtn = new JButton();
		saveBtn.addActionListener(this);

		disDirL = new JLabel();

		envDirL = new JLabel();

		scenDirL = new JLabel();

		disDirT = new JTextField();
		disDirT.setEditable(false);
		disDirT.setColumns(10);

		envDirT = new JTextField();
		envDirT.setEditable(false);
		envDirT.setColumns(10);

		scenDirT = new JTextField();
		scenDirT.setEditable(false);
		scenDirT.setColumns(10);

		disDirBtn = new JButton("...");
		disDirBtn.addActionListener(this);
		envDirBtn = new JButton("...");
		envDirBtn.addActionListener(this);
		scenDirBtn = new JButton("...");
		scenDirBtn.addActionListener(this);

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.TRAILING,
																								false)
																						.addComponent(
																								scenDirL,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								envDirL,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								disDirL,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								langL,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								126,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								groupLayout
																										.createSequentialGroup()
																										.addComponent(
																												scenDirT,
																												GroupLayout.DEFAULT_SIZE,
																												493,
																												Short.MAX_VALUE)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												scenDirBtn,
																												GroupLayout.PREFERRED_SIZE,
																												45,
																												GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								groupLayout
																										.createSequentialGroup()
																										.addComponent(
																												envDirT,
																												GroupLayout.DEFAULT_SIZE,
																												493,
																												Short.MAX_VALUE)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												envDirBtn,
																												GroupLayout.PREFERRED_SIZE,
																												45,
																												GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								groupLayout
																										.createSequentialGroup()
																										.addComponent(
																												disDirT,
																												GroupLayout.DEFAULT_SIZE,
																												493,
																												Short.MAX_VALUE)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												disDirBtn))
																						.addComponent(
																								langCB,
																								GroupLayout.PREFERRED_SIZE,
																								116,
																								GroupLayout.PREFERRED_SIZE)))
														.addComponent(
																saveBtn,
																GroupLayout.PREFERRED_SIZE,
																194,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(langL)
														.addComponent(
																langCB,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(disDirL)
														.addComponent(
																disDirT,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(disDirBtn))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(envDirL)
														.addComponent(
																envDirT,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(envDirBtn))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(scenDirL)
														.addComponent(
																scenDirT,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																scenDirBtn))
										.addPreferredGap(
												ComponentPlacement.RELATED,
												348, Short.MAX_VALUE)
										.addComponent(saveBtn)
										.addContainerGap()));
		setLayout(groupLayout);
		updateLanguage();
		loadData();
		this.repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		JFileChooser fc = new JFileChooser(Constants.USERDIR);
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		if (source == saveBtn) {
			saveData();
		} else if (source == disDirBtn) {
			fc.setSelectedFile(new File(props.getProperty("diseaseDir",
					"Disease")));
			if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				disDirT.setText(fc.getSelectedFile().getAbsolutePath());
			}
		} else if (source == envDirBtn) {
			fc.setSelectedFile(new File(props.getProperty("environmentDir",
					"Environment")));
			if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				envDirT.setText(fc.getSelectedFile().getAbsolutePath());
			}
		} else if (source == scenDirBtn) {
			fc.setSelectedFile(new File(props.getProperty("scenarioDir",
					"Scenario")));
			if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				scenDirT.setText(fc.getSelectedFile().getAbsolutePath());
			}
		}

	}

	private void loadData() {
		ArrayList<String> langs = lp.getSupportedLanguages();
		String temp = "";
		String lang = props.getProperty("lang");
		int index = 0;
		for (int i = 0; i < langs.size(); i++) {
			if (langs.get(i).equals(lang)) {
				index = i;
			}
			Locale l = new Locale(langs.get(i));
			temp = l.getDisplayName();
			langCB.addItem(temp);
		}
		langCB.setSelectedIndex(index);
		disDirT.setText(props.getProperty("diseaseDir", "Disease"));
		envDirT.setText(props.getProperty("environmentDir", "Environment"));
		scenDirT.setText(props.getProperty("scenarioDir", "Scenario"));
	}

	public boolean checkForChanges() {
		// TODO aktuelle Einstellungen mit
		return false;
	}

	/**
	 * @return status of confirming dialog
	 */
	public boolean confirmDialog() {
		int result = JOptionPane.showConfirmDialog(this,
				lp.getLocalizedString("confirmChanges"),
				lp.getLocalizedString("confirmChangesTitle"),
				JOptionPane.YES_NO_OPTION);
		if (result == JOptionPane.YES_OPTION) {
			return saveData();
		}
		return true;
	}

	public boolean saveData() {
		Locale l = new Locale(lp.getSupportedLanguages().get(
				langCB.getSelectedIndex()));
		lp.setLang(l);
		props.setProperty("lang", l.getLanguage());
		props.setProperty("diseaseDir", disDirT.getText());
		props.setProperty("environmentDir", envDirT.getText());
		props.setProperty("scenarioDir", scenDirT.getText());
		try {
			props.store(new FileWriter("Wusel.properties"), "");
		} catch (IOException e) {
			WuselLogger.log.error(e.getLocalizedMessage());
			return false;
		}
		return true;
	}

	@Override
	public void updateLanguage() {
		langL.setText(lp.getLocalizedString("lang"));

		disDirL.setText(lp.getLocalizedString("diseaseDir"));
		envDirL.setText(lp.getLocalizedString("environmentDir"));
		scenDirL.setText(lp.getLocalizedString("scenarioDir"));

		saveBtn.setText(lp.getLocalizedString("save"));
	}
}
